import { defineStore } from 'pinia'
import Cache from '@/support/cache'

interface exceptions {
  message: string
  trace: Array<object>
}

/**
 * app
 */
type app = {
  size: 'small' | 'medium' | 'large'

  isExpand: boolean

  locale: 'zh' | 'en'

  isMobile: boolean

  isDarkMode: boolean

  activeMenu: string

  isExceptionShow: boolean

  exceptions: exceptions

  serverPrint: string

  is_vertical: boolean
}

export const useAppStore = defineStore('app', {
  state: (): app => ({
    size: 'small',
    isExpand: true,
    locale: Cache.get('language'),
    isMobile: false,
    isDarkMode: false,
    activeMenu: '/dashboard',
    isExceptionShow: false,
    exceptions: {
      message: '',
      trace: []
    },
    serverPrint: '',
    is_vertical: false
  }),

  getters: {
    getSize(): string {
      return this.size
    },

    getLocale(): string {
      return this.locale
    },

    getIsMobile(): boolean {
      return this.isMobile
    },

    getIsDarkMode(): boolean {
      return this.isDarkMode
    },

    getActiveMenu(): string {
      return this.activeMenu
    },

    getExceptionShow(): boolean {
      return this.isExceptionShow
    },

    getExceptionTrace(): object | null {
      return this.exceptions
    },

    getServerPring(): string {
      return this.serverPrint
    },

    getVertical(): boolean {
      return this.is_vertical
    }
  },

  actions: {
    changeSize(size: 'small' | 'medium' | 'large'): void {
      this.size = size
    },

    changeLocale(locale: 'zh' | 'en'): void {
      Cache.set('language', locale)

      this.locale = locale
    },

    changeExpaned(): void {
      this.isExpand = !this.isExpand
    },

    setDarkMode(isDarkMode: boolean): void {
      this.isDarkMode = isDarkMode
    },

    setActiveMenu(activeMenu: string): void {
      this.activeMenu = activeMenu.startsWith('/') ? activeMenu : '/' + activeMenu
    },

    setIsExceptionShow(show: boolean, exceptions: exceptions, serverPrint: string): void {
      this.isExceptionShow = show
      this.exceptions = exceptions
      this.serverPrint = serverPrint
    },

    setVertical(isVertical = false) {
      this.is_vertical = isVertical
    }
  }
})
